/*
 * mainserver.cpp
 *
 *  Created on: 2011-8-16
 *      Author: chenl
 *     Purpose:
 *        Note:
 */

#include "mainserver.h"
#include "config.h"
#include "common/type_def.h"
#include "common/dataqueue.h"

CMainServer::CMainServer() : m_command(enmCommand_Nop)
{

}

CMainServer::~CMainServer()
{

}

int32_t CMainServer::Initialize()
{
	int32_t ret = S_OK;

	ret = CREATE_CONFIG_INSTANCE().Initialize();
	if (0 > ret)
	{
		WRITE_SERVER_LOG(enmLogLevel_ERROR, "Initialize config failed!ret=%d", ret);
		return ret;
	}

	ret = CREATE_RADARDATAQUEUE_INSTANCE().Initialize();
	if (0 > ret)
	{
		WRITE_SERVER_LOG(enmLogLevel_ERROR, "Initialize radar data queue failed!ret=%d", ret);
		return ret;
	}

	ret = GET_CLIENTDATAQUEUE_INSTANCE().Initialize();
	if (0 > ret)
	{
		WRITE_SERVER_LOG(enmLogLevel_ERROR, "Initialize client data queue failed!ret=%d", ret);
		return ret;
	}
	return S_OK;
}

int32_t CMainServer::Uninitialize()
{
	int32_t ret = S_OK;

	ret = GET_CLIENTDATAQUEUE_INSTANCE().Uninitialize();
	if (0 > ret)
	{
		WRITE_SERVER_LOG(enmLogLevel_ERROR, "Uninitialize client data queue failed!ret=%d", ret);
		return ret;
	}

	ret = CREATE_RADARDATAQUEUE_INSTANCE().Uninitialize();
	if (0 > ret)
	{
		WRITE_SERVER_LOG(enmLogLevel_ERROR, "Uninitialize radar data queue failed!ret=%d", ret);
		return ret;
	}

	ret = CREATE_CONFIG_INSTANCE().Uninitialize();
	if (0 > ret)
	{
		WRITE_SERVER_LOG(enmLogLevel_ERROR, "Uninitialize config failed!ret=%d", ret);
		return ret;
	}
	return S_OK;
}

void CMainServer::Terminate()
{
	m_command = enmCommand_Term;
}

void CMainServer::Run()
{
	m_logThread.Start();
	m_IOThread.Start();

	while (true)
	{
		switch (m_command)
		{
		case enmCommand_Reload:
			Reload();
			break;
		case enmCommand_Term:
			return;
		}
		m_command = enmCommand_Nop;
	}

}

int32_t CMainServer::Reload()
{
	return Initialize();
}



